//
//  AppDelegate.m
//  仁达理财
//
//  Created by yuanmc on 16/7/25.
//  Copyright © 2016年 六点科技. All rights reserved.
//

#import "AppDelegate.h"
#import "RGGNavigationController.h"
#import "RGGTabBarController.h"
#import "KeychainItemWrapper.h"
#import "WXApi.h"
@interface AppDelegate ()<WXApiDelegate>

@end

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    
    
    [WXApi registerApp:@"wx3e8655c378a557d0"];
    self.window = [[UIWindow alloc]init];
    
    self.window.frame = [UIScreen mainScreen].bounds;
// 
    RGGNavigationController *nav = [[RGGNavigationController alloc]initWithRootViewController:[[LoginViewController alloc]init]];
////
    self.window.rootViewController = nav;
    NSLog(@"fefsdfsfsdf%f",50/3.0);
//    self.window.rootViewController = [[RGGTabBarController alloc]init];
    
    [self.window makeKeyAndVisible];
    //获取唯一标识
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
         [self getuuid];
    });
    
//   [self showGuiView];
    
    return YES;
}

- (void)getuuid{
    //    添加设备唯一标识
    
    KeychainItemWrapper *wrapper = [[KeychainItemWrapper alloc] initWithIdentifier:@"kUUID" accessGroup:nil];
    NSString *aliaStr = [wrapper objectForKey:(id)CFBridgingRelease(kSecValueData)];
    
    if (aliaStr.length > 0) {
        NSLog(@"*********%@",aliaStr);
    } else {
        aliaStr = [FactoryTool getUniqueclientID];
        //保存数据
        [wrapper resetKeychainItem];
        [wrapper setObject:aliaStr forKey:(id)CFBridgingRelease(kSecValueData)];
        
    }

}


- (void)applicationWillResignActive:(UIApplication *)application {
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}

- (void)applicationDidEnterBackground:(UIApplication *)application {
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}

- (void)applicationWillEnterForeground:(UIApplication *)application {
    // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}

- (void)applicationDidBecomeActive:(UIApplication *)application {
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}

- (void)applicationWillTerminate:(UIApplication *)application {
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}
- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url
{
    return  [WXApi handleOpenURL:url delegate:self];
}

- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation
{
    BOOL isSuc = [WXApi handleOpenURL:url delegate:self];
    NSLog(@"url %@ isSuc %d",url,isSuc == YES ? 1 : 0);
    return  isSuc;
}

-(void)onReq:(BaseReq *)req{
    NSLog(@"onReq%@",req);
    
}

-(void)onResp:(BaseResp *)resp{
    NSLog(@"resp%@",resp);
    if([resp isKindOfClass:[SendMessageToWXResp class]])
    {   // 0    成功  展示成功页面
        //-1    错误  可能的原因：签名错误、未注册APPID、项目设置APPID不正确、注册的APPID与设置的不匹配、其他异常等。
        //-2    用户取消    无需处理。发生场景：用户不支付了，点击取消，返回APP。
        if(resp.errCode == 0){//分享成功
            
            NSString *strTitle = [NSString stringWithFormat:@"ok"];
            NSString *strMsg = [NSString stringWithFormat:@"errcode:%d", resp.errCode];

            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:strTitle message:strMsg delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
            [alert show];
            
            
        }
        
    }
}

-(void)weiXinShare{
    
    WXMediaMessage *message = [WXMediaMessage message];
    [message setThumbImage:[UIImage imageNamed:@"123"]];
    
    WXImageObject *ext = [WXImageObject object];
    NSString *filePath = [[NSBundle mainBundle] pathForResource:@"X" ofType:@"png"];
    NSLog(@"filepath :%@",filePath);
    ext.imageData = [NSData dataWithContentsOfFile:filePath];
    
    //UIImage* image = [UIImage imageWithContentsOfFile:filePath];
    UIImage* image = [UIImage imageWithData:ext.imageData];
    ext.imageData = UIImagePNGRepresentation(image);
    
    //    UIImage* image = [UIImage imageNamed:@"res5thumb.png"];
    //    ext.imageData = UIImagePNGRepresentation(image);
    
    message.mediaObject = ext;
    
    SendMessageToWXReq* req = [[SendMessageToWXReq alloc] init];
    req.bText = NO;
    req.message = message;
    req.scene = WXSceneTimeline;
    
    [WXApi sendReq:req];
    
    
}
@end
